// pages/my/pay/pay_sure.js
var commision_userIds;
var canpay=true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    weixin: false,
    yue: true,
    info: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var info = JSON.parse(options.info);
    commision_userIds = options.commision_userIds;
    console.log("commision_userIds" + commision_userIds)
    this.setData({
      info: info
    })
  },


  weixin: function() {
    this.setData({
      weixin: true
    })
  },

  yue: function() {
    this.setData({
      yue: true
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },


  radioChange: function(e) {
    var sele = e.detail.value;
    console.log(sele)
  },
  paysure: function(e) {
    //获取推送消息fromID
    if (!canpay) {
      return
    }
    canpay = !canpay;
    let formId = e.detail.formId;
    console.log('form发生了submit事件，推送码为：', formId);
    var vm = this
    var dataparm
    console.log("commision_userIds2" + commision_userIds)
    if (commision_userIds != undefined) {
      dataparm = {
        "shareUserId": commision_userIds,
        "balance": vm.data.info.totalCount,
        "programId": vm.data.info.id,
        "userId": wx.getStorageSync("userId")
      }
    } else {
      dataparm = {
        "balance": vm.data.info.totalCount,
        "programId": vm.data.info.id,
        "userId": wx.getStorageSync("userId")
      }
    }
    getApp().requestLoading(getApp().globalData.https + "orderResource/walletRecord",
      "post", dataparm, "支付中...",
      function(res) {
        console.log("支付结果" + JSON.stringify(res));
        if (res.code == "014") {
          setTimeout(function() {
            getApp().showMsgToast(res.message)
          },500)
          setTimeout(function() {
            canpay = !canpay;
            wx.navigateTo({
              url: "../../my/my_wallet/my_wallet",
              success: function() {},
              fail: function() {}
            })
          }, 2000)
        } else if (res.code != "0") {
          setTimeout(function () {
            getApp().showMsgToast(res.message)
          }, 500)
          canpay = !canpay;

        } else if (res.code == "0") {
          wx.showToast({
            title: "支付成功",
          })
          let pages = getCurrentPages();
          var currPage = pages[pages.length - 2]; //上一页
          currPage.setData({
            isrefresh: true
          })
          var parm = {
            "formId": formId,
            "mark": "通知",
            "name": vm.data.info.title,
            "pageIndex": "pages/style/style",
            "price": Math.round(vm.data.info.totalCount),
            "userId": wx.getStorageSync("userId")
          }
          getApp().request(getApp().globalData.https + "customer/purchase/notice",
            "post", parm,
            function(res) {
              setTimeout(function() {
                canpay = !canpay;
                wx.navigateBack()
              }, 1000)
            }
          )
        }
      }
    )
  },
  //封装提交的数据
  //   var datas = {
  //     "amount": 1,
  //     "body": "天天必听听币充值",
  //     "discountCount": 1,
  //     "discountStatus": 1,
  //     "payWay": 1,
  //     "price": "1",
  //     "totalPay": "1",
  //     "userId": 34
  //   }
  //   getApp().requestLoading(getApp().globalData.https + "orderResource/order",
  //     "post", datas, "加载中...",
  //     function(res) {
  //       var r = res.data
  //       if (res.code == 0) {
  //         wx.requestPayment({
  //           'timeStamp': r.timeStamp,
  //           'nonceStr': r.nonceStr,
  //           'package': "prepay_id=" + r.package,
  //           'signType': 'MD5',
  //           'paySign': r.paySign,
  //           'success': function(res) {
  //             wx.showToast({
  //               title: '支付成功',
  //               icon: 'none',
  //               duration: 2000
  //             })
  //           },
  //           'fail': function(res) {
  //             console.log(res)
  //             wx.showToast({
  //               title: '支付失败',
  //               icon: 'none',
  //               duration: 2000
  //             })
  //           }
  //         })
  //       }
  //     });
  // }
})